package com.bridgeintelligent.tag.webserver.dim.mgmt.service.report;

import com.bridgeintelligent.tag.webserver.dim.mgmt.api.BaseCode;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.report.BannerInfo;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.report.ReportRep;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@Api(tags = "轮播图信息接口")
@RestController
@RequestMapping("/api/banner")
@AllArgsConstructor
@Slf4j
public class BannerController {

    private BannerService  bannerService;

    @ApiOperation(value = "添加轮播信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "content", value = "轮播信息", required = true),
            @ApiImplicitParam(paramType = "query", name = "contentType", value = "信息信息", required = true)
    })
    @GetMapping("/add")
    public ReportRep add(String content,String contentType) {
        try {
           bannerService.addBanner(new BannerInfo().setContent(content).setContentType(contentType));
            return new ReportRep<>(BaseCode.SUCCESS,"添加成功");
        } catch (Exception e) {
            log.info("添加轮播信息失败:"+e);
            return new ReportRep<>(new ArrayList<>(),-1, "添加失败");
        }
    }

    @ApiOperation(value = "修改轮播信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "content", value = "轮播信息", required = true),
            @ApiImplicitParam(paramType = "query", name = "contentType", value = "信息信息", required = true),
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键", required = true)
    })
    @GetMapping("/update")
    public ReportRep update(String id,String content,String contentType) {
        try {
            bannerService.updateBanner(new BannerInfo().setId(id).setContent(content).setContentType(contentType));
            return new ReportRep<>(BaseCode.SUCCESS,"修改成功");
        } catch (Exception e) {
            log.info("修改轮播信息失败:"+e);
            return new ReportRep<>(new ArrayList<>(),-1, "修改失败");
        }
    }

    @ApiOperation(value = "查询轮播信息列表")
    @ApiImplicitParams({})
    @GetMapping("/list")
    public ReportRep list() {
        try {
            List<BannerInfo> bannerInfos = bannerService.listBanner();
            return new ReportRep<>(bannerInfos,BaseCode.SUCCESS,"查询轮播信息列表成功");
        } catch (Exception e) {
            log.info("查询轮播信息列表失败:"+e);
            return new ReportRep<>(new ArrayList<>(),-1, "查询轮播信息列表失败");
        }
    }

    @ApiOperation(value = "查询轮播信息的详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键", required = true)
    })
    @GetMapping("/detail")
    public ReportRep detail(String id) {
        try {
            BannerInfo one = bannerService.findOne(id);
            return new ReportRep<>(one,BaseCode.SUCCESS,"查询轮播信息详情成功");
        } catch (Exception e) {
            log.info("查询轮播信息详情失败:"+e);
            return new ReportRep<>(new ArrayList<>(),-1, "查询轮播信息详情失败");
        }
    }

    @ApiOperation(value = "删除轮播信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键", required = true)
    })
    @DeleteMapping("/delete")
    public ReportRep delete(String id) {
        try {
            bannerService.deleteOne(id);
            return new ReportRep<>(BaseCode.SUCCESS,"删除轮播信息成功");
        } catch (Exception e) {
            log.info("删除轮播信息失败:"+e);
            return new ReportRep<>(new ArrayList<>(),-1, "删除轮播信息失败");
        }
    }


}
